package com.qg.cyapp.adapter;

import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qg.cyapp.R;
import com.qg.cyapp.bean.FoodBean;
import com.qg.cyapp.util.ShareUtil;

public class MenuSingleAdapter extends PagerAdapter {

	private Context context;
	private List<FoodBean> list;
	private LayoutInflater inflater;

	public MenuSingleAdapter(Context context, List<FoodBean> list) {
		// TODO Auto-generated constructor stub
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.list = list;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (list.size() == 1) {
			return 1;
		}

		return Integer.MAX_VALUE;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}

	// 当要显示的图片可以进行缓存的时候，会调用这个方法进行显示图片的初始化，我们将要显示的ImageView
	// 加入到ViewGroup中，然后作为返回值返回即可
	@Override
	public Object instantiateItem(ViewGroup view, int position) {
		int curPosition = position % (list.size());
		Log.v("position:", "position:" + position + "current:" + curPosition);
		FoodBean food = list.get(curPosition);
		View content = inflater.inflate(R.layout.item_food_single, null);
		ImageView iv = (ImageView) content.findViewById(R.id.iv_food);
		ShareUtil.imageLoader.displayImage(food.picture, iv, null, false);
		TextView tvFoodPrice = (TextView) content
				.findViewById(R.id.tv_food_price);
		TextView tvFoodBom = (TextView) content.findViewById(R.id.tv_food_bom);
		TextView tvFoodSure = (TextView) content
				.findViewById(R.id.tv_food_sure);
		TextView tvFoodTaboo = (TextView) content
				.findViewById(R.id.tv_food_taboo);
		tvFoodPrice.setText("价格:  " + String.valueOf(food.price) + "元");
		String foodBom = "配料:  " + (food.ingredients==null?"无":food.ingredients);
		tvFoodBom.setText(foodBom);
		if (food.function != null )
			tvFoodSure.setText("食材功效:  " + food.function);
		String foodWarning = "禁忌:  " + (food.warning==null?"无":food.warning) + "。          ";
		tvFoodTaboo.setText(foodWarning);
		view.addView(content);
		return content;
	}

	@Override
	public void destroyItem(ViewGroup view, int position, Object object) {
		view.removeViewInLayout((View) object);
	}

}
